package com.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.BankEntity.AwardCreditInfo;
import com.BankEntity.BasicInfo;
import com.BankEntity.ContratInfo;
import com.BankEntity.CreditAppOut;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.model.task.TaskState;
import com.bstek.uflo.query.TaskQuery;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.StartProcessInfo;
import com.bstek.uflo.service.TaskService;
import com.bstek.urule.runtime.KnowledgePackage;
import com.bstek.urule.runtime.KnowledgeSession;
import com.bstek.urule.runtime.KnowledgeSessionFactory;
import com.bstek.urule.runtime.service.KnowledgeService;

@Component
public class TestPR {

	@Autowired
	private KnowledgeService knowledgeService;

	@Autowired
	private ProcessService processService;

	@Autowired
	private TaskService taskService;

	/**
	 * 知识包调用
	 * 
	 * @return
	 * @throws IOException
	 */
	@Expose
	public CreditAppOut executeUrule() throws IOException {
		KnowledgePackage kp = this.loadKnowledgePackage();
		KnowledgeSession ks = KnowledgeSessionFactory.newKnowledgeSession(kp);
		BasicInfo basicInfo = new BasicInfo();
		basicInfo.setApplicationtype("主卡"); // 申请类型
		basicInfo.setAgebycredit(50);// 主卡申请人年龄
		basicInfo.setAcage(70);// 附卡申请人年龄
		basicInfo.setCardNo("666666");// 主卡申请人证件号码
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("blackUser", "1");// 模拟 黑名单命中
		params.put("noRefusal", "是");// 模拟 免拒绝标识
		List<String> listResault = new ArrayList<String>();
		params.put("uruleResault", listResault); // 参数中的 规则返回结果
		// 因是List类型，开始规则时需要初始化一个List对象，否则会报错
		List<String> listhisRefuse = new ArrayList<String>();
		params.put("hisRefuseList", listhisRefuse);// 参数中的 历史拒绝列表
		// 由BasicInfo对象的hisRefuseList属性遍历得到的cardNo集合
		CreditAppOut out = new CreditAppOut();// 规则中使用的对象 都要在开始规则前初始化
		ks.insert(basicInfo);
		ks.insert(out);
		ks.startProcess("ruleFlow", params);
		return out;
	}

	/**
	 * 开启流程
	 */
	@Expose
	public void startFlow() {
		StartProcessInfo startProcessInfo = new StartProcessInfo(ContextHolder.getLoginUserName());
		startProcessInfo.setBusinessId("TestUsernameOne");
		BasicInfo basicInfo = new BasicInfo();
		basicInfo.setApplicationtype("主卡"); // 申请类型
		basicInfo.setAgebycredit(50);// 主卡申请人年龄
		basicInfo.setAcage(70);// 附卡申请人年龄
		basicInfo.setCardNo("666666");// 主卡申请人证件号码
		basicInfo.setPostLevel("处级");
		basicInfo.setAreaLevel("省级/市级");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("blackUser", "1");// 模拟 黑名单命中
		params.put("noRefusal", "是");// 模拟 免拒绝标识
		List<String> listResault = new ArrayList<String>();
		params.put("uruleResault", listResault); // 参数中的 规则返回结果
		// 因是List类型，开始规则时需要初始化一个List对象，否则会报错
		List<String> listhisRefuse = new ArrayList<String>();
		params.put("hisRefuseList", listhisRefuse);// 参数中的 历史拒绝列表
		int dutyBase = 0; // 根据职务级别 判断职务基数
		double localRatio = 0d;// 根据区域级别 判断 地域系数
		switch (basicInfo.getPostLevel()) {
		case "行政事业单位厅局级以上":
			dutyBase = 50000;
			break;
		case "处级":
			dutyBase = 40000;
			break;
		case "科级、科员及其他种类":
			dutyBase = 30000;
			break;
		}
		switch (basicInfo.getAreaLevel()) {
		case "省级/市级":
			localRatio = 1;
			break;
		case "区级":
			localRatio = 0.95;
			break;
		case "县级/县级市":
			localRatio = 0.9;
			break;
		}
		params.put("dutyBase", dutyBase);// 职位基数
		params.put("localRatio", localRatio); // 地域系数
		// 由BasicInfo对象的hisRefuseList属性遍历得到的cardNo集合
		CreditAppOut out = new CreditAppOut();// 规则中使用的对象 都要在开始规则前初始化
		ContratInfo conInfo = new ContratInfo();// 贷款信息
		AwardCreditInfo awardInfo = new AwardCreditInfo();// 信用卡信息

		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("basicInfo", basicInfo);
		parameter.put("creditAppOut", out);
		parameter.put("uruleProcessParam", params);
		parameter.put("contratInfo", conInfo);
		parameter.put("awardCreditInfo", awardInfo);
		startProcessInfo.setVariables(parameter);
		processService.startProcessByName("BankFlow", startProcessInfo);
	}

	@Expose
	public void passAction() {
		List<Task> list = taskService.createTaskQuery().businessId("TestUsernameOne").addTaskState(TaskState.Ready)
				.list();
		Task task = list.get(0);
		taskService.start(task.getId());
		taskService.complete(task.getId());
	}

	private KnowledgePackage loadKnowledgePackage() throws IOException {
		return knowledgeService.getKnowledge("Bank/testBank");
	}

}
